//
// Created by pangy on 2025/5/7.
//
#include <stdio.h>
#include <limits.h>
#include "Status.h"

#define array_size 20
#define max_init INT_MAX

/**
 * 计算 i!*2^i
 * @param n
 * @return 计算结果
 */
Status calculate(int n, int a[]);

int main() {
    int n, a[array_size];

    n = 5;

    printf("计算 i! * 2^i...\n");
    Status ret = calculate(n, a);
    if (ret == OK) {
        printf("当n = %d时, a[n-1] = %d\n", n, a[n - 1]);
    } else if (ret == ERROR) {
        printf("数组越界");
    } else {
        printf("数值溢出");
    }

    return 0;
}

Status calculate(int n, int a[]) {
    if (n < 1 || n > array_size) {
        return ERROR;
    }

    a[0] = 1;

    for (int i = 1; i < n; ++i) {
        // 即 (2*i) * (i-1)! * 2^(i-1) = i! * 2^i > max_int
        if (max_init / (2 * i) < a[i - 1]) {
            return OVERFLOW;
        }
        a[i] = (2 * i) * a[i - 1];
    }
    return OK;
}